home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Movieless in QuickTime / PlayAnimatedGIF / source / PlayMovie.c next >
Text File  |  1998-06-17  |  11KB  |  502 lines

  1. /*
  2.   File:            PlayMovie.c
  3.   Contains:        Movie Playing Application
  4.   Written by:    John Wang / DTS
  5.   Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  6.   Change History (most recent first):
  7.   <2>         12/4/94        khs        changed the format of the file to the new look and feel
  8.   <1>         11/12/91    JW        1.0 Completed
  9.   To Do:
  10. */
  11.  
  12.  
  13. // INCLUDES
  14. #include     <Gestalt.h>
  15. #include    <SegLoad.h>
  16. #include    <ToolUtils.h>
  17. #include     <Devices.h>
  18. #include    <Errors.h>
  19.  
  20. #include    <Movies.h>
  21.  
  22.  
  23. // DEFINES
  24. #define Gestalttest        0xA1AD
  25. #define NoTrap            0xA89F
  26.  
  27. #define    appleID            128            
  28. #define    appleMenu        0
  29. #define    aboutMeCommand    1
  30.  
  31. #define    fileID            129
  32. #define openCommand        1
  33. #define    flattenCommand    2
  34. #define closeCommand    3
  35. #define    quitCommand     5
  36.  
  37. #define    aboutMeDLOG        128
  38. #define    okButton        1
  39.  
  40. #define    MAXWINDOWS        5
  41.  
  42.  
  43. // FUNCTION PROTOTYPES
  44. Movie GetMovieFromFile(void);
  45. OSErr PlayMovie(int index);
  46. short Flatten(int index);
  47. void ShowAboutMeDialog(void);
  48. void Init(void);
  49. void Finish();
  50. void DoOpenCommand();
  51. void DoFlattenCommand();
  52. void DoCloseCommand();
  53. void DoCommand(long mResult);
  54. void PlayMovies(void);
  55.  
  56.  
  57. // GLOBALS
  58. Boolean DoneFlag = false;
  59. MenuHandle mymenu0, mymenu1;
  60. Boolean playingMovie[MAXWINDOWS];
  61. Movie myMovie[MAXWINDOWS];
  62. WindowPtr myWindow[MAXWINDOWS];
  63. int startlocation;
  64.  
  65.  
  66. // FUNCTIONS
  67. /*------------------------------------------------------*/
  68. /*    GetMovieFromFile().                                    */
  69. /*------------------------------------------------------*/
  70.  
  71. Movie GetMovieFromFile(void)
  72. {
  73.     OSErr err;
  74.     StandardFileReply reply;
  75.     Point where =
  76.     {
  77.         200,  50
  78.     };
  79.     SFTypeList types;
  80.     short movieResRefNum;
  81.     short actualResId;
  82.     Movie theMovie;
  83.  
  84.     types[0] = 'MooV';
  85.     StandardGetFilePreview(nil, 1, types, &reply);
  86.     if (!reply.sfGood)
  87.         return ((Movie)0);
  88.  
  89.     err = OpenMovieFile(&reply.sfFile, &movieResRefNum, fsRdPerm);
  90.     if (GetMoviesError())
  91.         return ((Movie)0);
  92.     if (err)
  93.         return ((Movie)0);
  94.     actualResId = DoTheRightThing;
  95.  
  96.     err = NewMovieFromFile(&theMovie, movieResRefNum, &actualResId, NULL, newMovieActive, (Boolean *)0);
  97.     if (GetMoviesError())
  98.         return ((Movie)0);
  99.     if (err)
  100.         return ((Movie)0);
  101.  
  102.     err = CloseMovieFile(movieResRefNum);
  103.     if (GetMoviesError())
  104.         return ((Movie)0);
  105.     if (err)
  106.         return ((Movie)0);
  107.  
  108.     return (theMovie);
  109. }
  110.  
  111. /*------------------------------------------------------*/
  112. /*    PlayMovie().                                            */
  113. /*------------------------------------------------------*/
  114.  
  115. OSErr PlayMovie(int index)
  116. {
  117.     Rect movieBounds;
  118.  
  119.     GetMovieBox(myMovie[index], &movieBounds);
  120.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  121.     if (movieBounds.right < 40)
  122.         movieBounds.right = 40;
  123.     if (movieBounds.bottom < 20)
  124.         movieBounds.bottom = 20;
  125.     SetMovieBox(myMovie[index], &movieBounds);
  126.     OffsetRect(&movieBounds, startlocation, startlocation);
  127.     myWindow[index] = NewCWindow(0L, &movieBounds, "\pAn Animated GIF", 1, 0, (WindowPtr) - 1, true, 0L);
  128.     startlocation += 50;
  129.     if (startlocation > 300)
  130.         startlocation = 50;
  131.  
  132.     SetMovieGWorld(myMovie[index], (CGrafPtr)myWindow[index], 0);
  133.  
  134.     if (GetMoviesError())
  135.         DebugStr("\pSetMovieGWorld error.");
  136.  
  137.     /*    Uncomment these lines if you want to pre load the movie into ram.
  138.       GotoBeginningOfMovie(myMovie[index]);
  139.       if (LoadMovieIntoRam(myMovie[index], GetMovieTime(myMovie[index], 0L),
  140.       GetMovieDuration(myMovie[index]),
  141.       0) != noErr)
  142.       DebugStr("\PNot enough memory to load movie into ram.");
  143.     */
  144.     SetMovieRate(myMovie[index], 0x00010000);
  145.  
  146.     return noErr;
  147. }
  148.  
  149. /*------------------------------------------------------*/
  150. /*    Flatten().                                            */
  151. /*------------------------------------------------------*/
  152.  
  153. short Flatten(int index)
  154. {
  155.     StandardFileReply reply;
  156.     OSErr theErr = noErr;
  157.  
  158.     StandardPutFile("\pName of flattened movie.", "\pUntitled", &reply);
  159.     if (!reply.sfGood)
  160.         return fnOpnErr;
  161.  
  162.     theErr = GetMoviesError();
  163.     if (theErr != noErr)
  164.         DebugStr("\pCall Before FlattenMovies failed.");
  165.  
  166.     FlattenMovie(myMovie[index], flattenAddMovieToDataFork, &reply.sfFile, 'JWJW', 0, createMovieFileDeleteCurFile, nil, nil);
  167.  
  168.     theErr = GetMoviesError();
  169.     if (theErr != noErr)
  170.         DebugStr("\pFlattenMovies failed.");
  171.  
  172.     return (theErr);
  173. }
  174.  
  175. /*------------------------------------------------------*/
  176. /*    ShowAboutMeDialog()                                    */
  177. /*------------------------------------------------------*/
  178.  
  179. void ShowAboutMeDialog(void)
  180. {
  181.     GrafPtr savePort;
  182.     DialogPtr theDialog;
  183.     short itemHit;
  184.  
  185.     GetPort(&savePort);
  186.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) - 1);
  187.     SetPort(theDialog);
  188.  
  189.     do
  190.     {
  191.         ModalDialog(nil, &itemHit);
  192.     } while (itemHit != okButton);
  193.  
  194.     CloseDialog(theDialog);
  195.  
  196.     SetPort(savePort);
  197.     return;
  198. }
  199.  
  200. /*------------------------------------------------------*/
  201. /*    Init().                                                */
  202. /*------------------------------------------------------*/
  203.  
  204. void Init(void)
  205. {
  206.     OSErr err;
  207.     int i;
  208.     long QDfeature,
  209.      OSfeature;
  210.  
  211.     /*    Initialize Managaer.    */
  212.     InitGraf(&qd.thePort);
  213.     FlushEvents(everyEvent, 0);
  214.     InitWindows();
  215.     InitDialogs(nil);
  216.     InitCursor();
  217.  
  218.     /*    Set up menus.    */
  219.     mymenu0 = GetMenu(appleID);
  220.     AddResMenu(mymenu0, 'DRVR');
  221.     InsertMenu(mymenu0, 0);
  222.     mymenu1 = GetMenu(fileID);
  223.     InsertMenu(mymenu1, 0);
  224.     DrawMenuBar();
  225.  
  226.     /*    Set up variables.    */
  227.     startlocation = 50;
  228.     for (i = 0; i < MAXWINDOWS; i++)
  229.     {
  230.         playingMovie[i] = false;
  231.         myWindow[i] = nil;
  232.     }
  233.  
  234.     /*    Use Gestalt to find if QuickDraw and QuickTime is available.    */
  235.     if ((GetTrapAddress(Gestalttest) != GetTrapAddress(NoTrap)))
  236.     {
  237.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  238.         if (err)
  239.             ExitToShell();
  240.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  241.         if (err)
  242.             ExitToShell();
  243.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0607)
  244.             ExitToShell();
  245.         err = Gestalt(gestaltQuickTime, &QDfeature);
  246.         if (err)
  247.             ExitToShell();
  248.     }
  249.     else
  250.         ExitToShell();
  251.  
  252.     /*    Open QuickTime last.    */
  253.     err = EnterMovies();
  254.     if (err)
  255.         ExitToShell();
  256. }
  257.  
  258. /*------------------------------------------------------*/
  259. /*    Finish().                                            */
  260. /*------------------------------------------------------*/
  261.  
  262. void Finish()
  263. {
  264.     ExitMovies();
  265.     ExitToShell();
  266. }
  267.  
  268. /*------------------------------------------------------*/
  269. /*    DoOpenCommand().                                        */
  270. /*------------------------------------------------------*/
  271.  
  272. void DoOpenCommand()
  273. {
  274.     int i,
  275.      useThisIndex;
  276.  
  277.     useThisIndex = -1;
  278.  
  279.     /*    Search for the first window that is nil.    */
  280.     for (i = MAXWINDOWS - 1; i >= 0; i--)
  281.         if (myWindow[i] == nil)
  282.             useThisIndex = i;
  283.  
  284.         /*    If index = -1, then it means that there are no windows avaiable.    */
  285.     if (useThisIndex != -1)
  286.     {
  287.         myMovie[useThisIndex] = GetMovieFromFile();
  288.         if (myMovie[useThisIndex] != 0)
  289.         {
  290.             PlayMovie(useThisIndex);
  291.             playingMovie[useThisIndex] = true;
  292.         }
  293.     }
  294. }
  295.  
  296. /*------------------------------------------------------*/
  297. /*    DoFlattenCommand().                                        */
  298. /*------------------------------------------------------*/
  299.  
  300. void DoFlattenCommand()
  301. {
  302.     int i;
  303.     WindowPtr myTempWindow;
  304.  
  305.     /*    Flatten movie that is currently selected.    */
  306.     myTempWindow = FrontWindow();
  307.     if (myTempWindow == nil)
  308.         return;
  309.     for (i = 0; i < MAXWINDOWS; i++)
  310.         if (myWindow[i] == myTempWindow)
  311.             Flatten(i);
  312. }
  313.  
  314. /*------------------------------------------------------*/
  315. /*    DoCloseCommand().                                        */
  316. /*------------------------------------------------------*/
  317.  
  318. void DoCloseCommand()
  319. {
  320.     int i;
  321.     WindowPtr myTempWindow;
  322.  
  323.     /*    Close selected window.    */
  324.     myTempWindow = FrontWindow();
  325.     if (myTempWindow == nil)
  326.         return;
  327.     for (i = 0; i < MAXWINDOWS; i++)
  328.         if (myWindow[i] == myTempWindow)
  329.         {
  330.             DisposeMovie(myMovie[i]);
  331.             DisposeWindow(myTempWindow);
  332.             playingMovie[i] = false;
  333.             myWindow[i] = nil;
  334.         }
  335. }
  336.  
  337. /*------------------------------------------------------*/
  338. /*    DoCommand().                                        */
  339. /*------------------------------------------------------*/
  340.  
  341. void DoCommand(long mResult)
  342. {
  343.     int theMenu;
  344.     short theItem;
  345.     Str255 daName;
  346.     GrafPtr savePort;
  347.  
  348.     theItem = LoWord(mResult);
  349.     theMenu = HiWord(mResult);
  350.  
  351.     switch (theMenu)
  352.     {
  353.         case appleID:
  354.             if (theItem == aboutMeCommand)
  355.                 ShowAboutMeDialog();
  356.             else
  357.             {
  358.                 GetMenuItemText(mymenu0, theItem, daName);
  359.                 GetPort(&savePort);
  360.                 (void)OpenDeskAcc(daName);
  361.                 SetPort(savePort);
  362.             }
  363.             break;
  364.  
  365.         case fileID:
  366.             switch (theItem)
  367.             {
  368.                 case openCommand:
  369.                     DoOpenCommand();
  370.                     break;
  371.                 case flattenCommand:
  372.                     DoFlattenCommand();
  373.                     break;
  374.                 case closeCommand:
  375.                     DoCloseCommand();
  376.                     break;
  377.                 case quitCommand:
  378.                     DoneFlag = true;
  379.                     break;
  380.                 default:
  381.                     break;
  382.             }
  383.             break;
  384.     }
  385.     HiliteMenu(0);
  386.     return;
  387. }
  388.  
  389. /*------------------------------------------------------*/
  390. /*    PlayMovies().                                            */
  391. /*------------------------------------------------------*/
  392.  
  393. void PlayMovies(void)
  394. {
  395.     int i;
  396.  
  397.     for (i = 0; i < MAXWINDOWS; i++)
  398.         if (playingMovie[i] == true)
  399.         {
  400.             if (IsMovieDone(myMovie[i]) == false)
  401.                 MoviesTask(myMovie[i], DoTheRightThing);
  402.             else
  403.             {
  404.                 DisposeMovie(myMovie[i]);
  405.                 DisposeWindow((WindowPtr)myWindow[i]);
  406.                 playingMovie[i] = false;
  407.                 myWindow[i] = nil;
  408.             }
  409.         }
  410. }
  411.  
  412.  
  413. /*------------------------------------------------------*/
  414. /*    main().                                                */
  415. /*------------------------------------------------------*/
  416.  
  417. void main(void)
  418. {
  419.     int i;
  420.     char key;
  421.     Boolean track;
  422.     EventRecord myEvent;
  423.     WindowPtr whichWindow;
  424.     int yieldTime;
  425.  
  426.  
  427.     Init();
  428.     yieldTime = 0;
  429.     for (;;)
  430.     {
  431.  
  432.         /*    We can't just do ExitToShell because we must cann ExitMovies.    */
  433.         if (DoneFlag)
  434.             Finish();
  435.  
  436.         /*    Play movies which are active.    */
  437.         PlayMovies();
  438.  
  439.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil))
  440.         {
  441.             switch (myEvent.what)
  442.             {
  443.                 case mouseDown:
  444.                     switch (FindWindow(myEvent.where, &whichWindow))
  445.                     {
  446.                         case inSysWindow:
  447.                             SystemClick(&myEvent, whichWindow);
  448.                             break;
  449.                         case inMenuBar:
  450.                             DoCommand(MenuSelect(myEvent.where));
  451.                             break;
  452.                         case inContent:
  453.                             SelectWindow(whichWindow);
  454.                             break;
  455.                         case inDrag:
  456.                             DragWindow(whichWindow, myEvent.where, &qd.screenBits.bounds);
  457.                             break;
  458.                         case inGrow:
  459.                             break;
  460.                         case inGoAway:
  461.                             track = TrackGoAway(whichWindow, myEvent.where);
  462.                             if (track)
  463.                                 DoCloseCommand();
  464.                             break;
  465.                         case inZoomIn:
  466.                             break;
  467.                         case inZoomOut:
  468.                             break;
  469.                         default:
  470.                             break;
  471.                     }
  472.                     break;
  473.                 case keyDown:
  474.                 case autoKey:
  475.                     key = myEvent.message & charCodeMask;
  476.                     if (myEvent.modifiers & cmdKey)
  477.                         if (myEvent.what == keyDown)
  478.                             DoCommand(MenuKey(key));
  479.                     break;
  480.                 case updateEvt:
  481.                     for (i = 0; i < MAXWINDOWS; i++)
  482.                         if ((WindowPtr)myEvent.message == myWindow[i])
  483.                         {
  484.                             BeginUpdate((WindowPtr)myWindow[i]);
  485.                             EndUpdate((WindowPtr)myWindow[i]);
  486.                         }
  487.                     break;
  488.                 case diskEvt:
  489.                     break;
  490.                 case activateEvt:
  491.                     break;
  492.                 case app4Evt:
  493.                     break;
  494.                 default:
  495.                     break;
  496.             }
  497.         }
  498.     }
  499. }
  500.  
  501.  
  502.